#include"http.hpp"



#define WWWROOT "./wwwroot"

std::string RequestStr(const HttpRequest& request){
    std::stringstream ss;
    ss<<request._method<<" "<<request._path<<" HTTP/"<<request._version<<"\r\n";
    for(auto& s:request._params)
    ss<<s.first<<"="<<s.second<<"\r\n";
    for(auto& header:request._headers){
        ss<<header.first<<": "<<header.second<<"\r\n";
    }
    ss<<"\r\n";
    ss<<request._body;
    return ss.str();
}
void Hello(const HttpRequest& request, HttpResponse* response){
    response->SetContent(RequestStr(request),"text/plain");
}
void Login(const HttpRequest& request, HttpResponse* response){
    response->SetContent(RequestStr(request),"text/plain");

}
void PutFile(const HttpRequest& request, HttpResponse* response){
    std::string filename=WWWROOT+request._path;
    Util::WriteFile(filename,request._body);

}
void DelFile(const HttpRequest& request, HttpResponse* response){
    response->SetContent(RequestStr(request),"text/plain");
}
int main()
{
   HttpServer server(8500);
   server.SetThreadCount(3);
   server.SetBaseDir(WWWROOT);//设置静态资源根目录
   server.Get("/hello",Hello);
   server.Post("/login",Login);
   server.Put("/1234.txt",PutFile);
   server.Delete("/1234.txt",DelFile);
   server.Listen();
   return 0;
}



